package com.megvii.koala.data.manager;

import com.megvii.corelib.util.DateTimeUtil;
import com.megvii.koala.data.bean.CreatureBean;
import com.megvii.koala.data.bean.UpPictureBean;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * Created by linyue on 15/12/27.
 */
public class CreatureManager<T extends CreatureBean> extends KoalaBeanManager<T> {
    private KoalaBeanManager searchManager;

    public CreatureManager() {
        super();
        searchManager = new KoalaBeanManager();
    }

    public CreatureManager(Comparator<T> comparator) {
        super(comparator);
        searchManager = new KoalaBeanManager(comparator);
    }

    public static void doWithCreatureName(CreatureBean creature) {
        if (creature == null) {
            return;
        }
        creature.pingyinName = creature.pinyin;//CharacterParser.getInstance().getSelling(creature.name);
        String nameFirst = "";
        if (creature.name != null && !creature.name.equals("null") && creature.name.length() > 0) {
            nameFirst = creature.name.substring(0, 1);
        }
        creature.nameFirst = nameFirst;
        String sortString = creature.pingyinName;

        String regEx = "[-_!@#$%^&*()_+<>?,.]";
        sortString = sortString.replaceAll(regEx, "");
        if (sortString.length() > 0) {
            sortString = sortString.substring(0, 1).toUpperCase();
        }

        // 正则表达式，判断首字母是否是英文字母
        if (sortString.matches("[A-Z]")) {
            creature.zimuName = sortString.toUpperCase();
        } else {
            creature.zimuName = "A";
        }
    }

    public void updateEx(T creature, boolean replaceDetectionPhoto) {
        if (creature == null) {
            return;
        }
        T original = get(creature.id);
        List<UpPictureBean> originalPhotos = new ArrayList<>();
        if (original != null) {
            originalPhotos = original.photos;
        }
        if (!replaceDetectionPhoto) {
            creature.photos = originalPhotos;
        }
        update(creature);
    }

    public void updateSearch(T creature, boolean replaceDetectionPhoto) {
        if (creature == null) {
            return;
        }
        T original = get(creature.id);
        List<UpPictureBean> originalPhotos = new ArrayList<>();
        if (original != null) {
            originalPhotos = original.photos;
        }
        if (!replaceDetectionPhoto) {
            creature.photos = originalPhotos;
        }
        if (super.get(creature.id) != null) {
            super.update(creature);
        }
        searchManager.update(creature);
    }

    @Override
    public void remove(String id) {
        super.remove(id);
        searchManager.remove(id);
    }

    @Override
    public T get(String id) {
        return super.get(id) == null ? (T) searchManager.get(id) : super.get(id);
    }

    public List<T> getSearchAll() {
        return searchManager.getAll();
    }

    public void clearSearch() {
        searchManager.clear();
    }

    @Override
    protected void update(T bean, boolean sortAndIndex) {
        filterCreatureContent(bean);
        doWithCreatureName(bean);
        super.update(bean, sortAndIndex);
    }

    public void filterCreatureContent(T creature) {
        if (creature == null) {
            return;
        }
        creature.avatar = makeUrl(creature.avatar);
        creature.start_time = DateTimeUtil.toMSTimestamp(creature.start_time);
        creature.end_time = DateTimeUtil.toMSTimestamp(creature.end_time);
        creature.test = DateTimeUtil.getFormattedDateTime(creature.start_time, false);
        if (creature.photos != null) {
            for (UpPictureBean pic : creature.photos) {
                pic.url = makeUrl(pic.url);
            }
        }
    }
}
